﻿var timeplay = 0;
var playcount = true;
function nhapnhay(selecter, thisObjtime, callback, Class, ClassActive) {
    var thisObj = $("#" + selecter);
    if (Class == undefined) {
        Class = "active";
    }
    if (ClassActive == undefined) {
        ClassActive = "active";   
    }
    function updateCountdown() {
        if (thisObjtime > 0) {
            thisObjtime -= 1;
            thisObj.addClass(Class);
            setTimeout(function () {
                thisObj.removeClass(Class);
                setTimeout(function () {
                    updateCountdown();
                }, 100);
            }, 100);
        } else {
            thisObj.removeClass("active");
            return callback();
        }
    }
    updateCountdown();
}
$(document).ready(function () {
  
    
});

function thongbao() {
    $("#Thongbao,.bgPopup").css("display", "block"); 
} 



function doicauhoi(Id) {
    PopupThongBao("THÔNG BÁO", "Bạn thực sự muốn đổi câu hỏi?", " <div class='span2'><a href='javascript:void(0)' title='Đóng'  onclick='popupNone()'>Không</a></div>"
                                   + "<div class='span2'><a href='javascript:void(0)' title='Đóng'  onclick='doicauhoi_b2(" + Id + ")'>Có</a></div>");
}
function doicauhoi_b2(question) {
    
    popupNone();
    timeplay = 2;
    questionHtml(question);    
   
    $("#btdoicauhoi").addClass("Use").html(""); 
}
function dungcuocchoi() {
    PopupThongBao("THÔNG BÁO", "Bạn thực sự muốn dùng cuộc chơi tại đây?", " <div class='span2'><a href='javascript:void(0)' title='Đóng'  onclick='popupNone()'>Không</a></div>"
                                   + "<div class='span2'><a href='javascript:void(0)' title='Đóng'  onclick='ketthuc()'>Có</a></div>");
}
//dungcuocchoi khangia doicauhoi trogiup50 khangia chuyengialst
function trogiup50(Id) {
   
    $("#bttrogiup50").addClass("Use").html("");
    playSound("sound5050");
    timeplay = 1;
    setTimeout(function () {
        $.ajax({
            url: "/Home/TroGiup50Json/",
            type: "Post",
            data: { "Id": Id },
            success: function (data) {
                timeplay = 0;
                $("#Choice" + data.sai1 + ",#Choice" + data.sai2).html("");
            }
        });
    }, 3200); 
}

function khangia(Id) {
    $("#btkhangia").addClass("Use").html("");
    $(".bgPopup").css("display", "block");
    playSound("khan_gia");
    timeplay =1;
    setTimeout(function () {
       
        $.ajax({
            url: "/Home/khangiaJson/",
            type: "Post",
            data: { "Id": Id }, 
            success: function (data) {
                $("#khangia,.bgPopup").css("display", "block");
                playSound("hoi_y_kien_chuyen_gia");
                updatekg(1, data.Choice1);
                updatekg(2, data.Choice2);
                updatekg(3, data.Choice3);
                updatekg(4, data.Choice4);  
            }
        });
    }, 5600); 
}
function updatekg(ChoiceId, Choice) {
    var thisObjtime = 0;
        function updateCountdown() {
            if (thisObjtime <= Choice) {
                thisObjtime += 1;
                $("#kg" + ChoiceId).css("height", thisObjtime + "%").find(".txt").text(thisObjtime+"%");
                setTimeout(function () {updateCountdown();}, 40);
            }
        }
    updateCountdown();
}
function chuyengialst() {
    $("#btchuyengialst").addClass("Use").html("");
    $("#chuyengialst,.bgPopup").css("display", "block");
    playSound("help_call");
    timeplay = 1;
    setTimeout(function () {
        playSound("help_callb");
    }, 1700);
}
function chuyengia(Id, cgId) {
    popupNone();
    $("#chuyengia,.bgPopup").css("display", "block");
    $.ajax({
        url: "/Home/chuyengiaJson/",
        type: "Post",
        data: { "Id": Id, "cgId": cgId },
        success: function (data) {
            var html = "";
            if (cgId == 1) {
                html+= "<a title='Bác sĩ' class='b" + cgId + "' >Bác sĩ</a>";
            } else if (cgId == 2) {
                html += "<a title='Giáo viên' class='b" + cgId + "' >Giáo viên</a>";
            } else if (cgId == 3) {
                html += "<a title='Kỹ sư' class='b" + cgId + "' >Kỹ sư</a>";
            } else if (cgId == 3) {
                html += "<a title='Phóng viên' class='b" + cgId + "' >Phóng viên</a>";
            }

            if (data.Answer == 1) {
                html += "<div class='msg'>Theo tôi đáp an đúng là<br/> A: " + data.Choice + "</div>";
            } else if (data.Answer == 2) {
                html += "<div class='msg'>Theo tôi đáp an đúng là<br/> B: " + data.Choice + "</div>";
            } else if (data.Answer == 3) {
                html += "<div class='msg'>Theo tôi đáp an đúng là<br/> C: " + data.Choice + "</div>";
            } else if (data.Answer == 4) {
                html += "<div class='msg'>Theo tôi đáp an đúng là<br/> D: " + data.Choice + "</div>";
            }
             
            + "<div class='button'><a href='javascript:void(0)' class='closePopup' title='Đóng'>Đóng</a></div>";
            $("#chuyengia .blockContent").html()
        }
    });
}
function PopupThongBao(tbTitle, tbMsg, tbHtml) {
    $("#tbTitle").html(tbTitle);
    $("#tbMsg").html(tbMsg);
    $("#tbHtml").html(tbHtml);
     
    $("#PopupThongbao,.bgPopup").css("display", "block");
}
function popupNone() {
    popupBgNone();
    popupNoneOnly();
    playSound("background_music", true);
    timeplay = 0;
}
function popupNoneOnly() {
    $(".popup").css("display", "none");
}
function popupBgNone() {
    $(".bgPopup").css("display", "none");
}


$(document).ready(function () {
    $(".closePopup").click(function () {
        popupNone();
    });  
});
//b1
function sansang() {
    loadSound("true_11");
    loadSound("true_21");
    loadSound("true_31");
    loadSound("true_41");
    loadSound("lose_11");
    loadSound("lose_21");
    loadSound("lose_31");
    loadSound("lose_41");
    loadSound("ans_11");
    loadSound("ans_21");
    loadSound("ans_31");
    loadSound("ans_41");
    loadSound("khan_gia");
    loadSound("call");
    loadSound("sound5050");
    loadSound("help_call");
    loadSound("help_callb");
    loadSound("hoi_y_kien_chuyen_gia");
    loadSound("out_of_time");
    loadSound("lose");



    touch_sound();
    setTimeout(function () {        
        mySwiper.swipeTo(3); 
        luatchoi(); 
    }, 1000); 
}
//b2
function luatchoi() { 
    playSound("luatchoi_b");
    loadSound("ready");
    setTimeout(function () {
        nhapnhay("q5",3, function () {
            nhapnhay("q10",3, function () {
                nhapnhay("q15", 3, function () {
                    setTimeout(function () {
                        ready();
                    }, 1500);
                });
            });
        });  
    },5000);
}
//b3
function ready() {
    playSound("ready");
    loadSound("gofind");
    setTimeout(function () {
        mySwiper.swipeTo(4); 
        thongbao();
    }, 2000);
}
//b4
function sansang2() {
    popupNone();
    playSound("gofind");
    loadSound("ques1");
    setTimeout(function () {
        loadSound("background_music");
         question(1); 
    },6000);
}
//b5

function question(question) {
    var soundid = question + 1;

     mySwiper.swipeTo(3);
     
     if (question == 3) {
         loadSound("important");         
     }
     if (question == 4) {
         loadSound("chuc_mung_vuot_moc_01_0");
     }
     if (question == 9) {
         loadSound("chuc_mung_vuot_moc_02_0");
     }
     if (question == 14) {
         loadSound("best_player");
     }
     loadSound("ques" + soundid);
     playSound("ques" + question);
     $("#lstquestion li").removeClass("selected");
     $(".box4 .bt_action span a").css("display", "block");
    nhapnhay("q" + question, 4, function () {
        $("#q" + question).addClass("selected");
    }, "selected", "selected");
   
    questionHtml(question); 
    setTimeout(function () {
        mySwiper.swipeTo(4);
        if (question == 5 || question == 10) {            
            playSound("important");
            setTimeout(function () {
                playSound("background_music", true);
            }, 3000);
        } else {
            playSound("background_music", true);
        } 
        new countdown("txttime", 30, function () {
            hetgio();
        });
    }, 3500);
}
//dungcuocchoi khangia doicauhoi trogiup50 khangia chuyengialst
function questionHtml(question) {
    $.ajax({ 
        url: "/Home/QuestionJson/", 
        type: "Post", 
        data: { "level": question },
        success: function (data) {
            $("#txtQuestion").text(data.Question);
            $("#txtQuestionId").text(" Câu hỏi " + question);
            $("#Choice1").html("<a  href='javascript:void(0)'  onclick='Answer(" + data.Id + ",1)'>A: <span>" + data.Choice1 + "</span></a>");
            $("#Choice2").html("<a  href='javascript:void(0)'  onclick='Answer(" + data.Id + ",2)'>B: <span>" + data.Choice2 + "</span></a>");
            $("#Choice3").html("<a  href='javascript:void(0)'  onclick='Answer(" + data.Id + ",3)'>C: <span>" + data.Choice3 + "</span></a>");
            $("#Choice4").html("<a  href='javascript:void(0)'  onclick='Answer(" + data.Id + ",4)'>D: <span>" + data.Choice4 + "</span></a>");
            $("#cg1").html("<a href='javascript:void(0)' title='Bác sĩ' class='b1' onclick='chuyengia(" + data.Id + ",1)'>Bác sĩ</a>");
            $("#cg2").html("<a href='javascript:void(0)' title='Giáo viên' class='b2' onclick='chuyengia(" + data.Id + ",2)'>Giáo viên</a>");
            $("#cg3").html("<a href='javascript:void(0)' title='Kỹ sư' class='b3' onclick='chuyengia(" + data.Id + ",3)'>Kỹ sư</a>");
            $("#cg4").html("<a href='javascript:void(0)' title='Phóng viên' class='b4' onclick='chuyengia(" + data.Id + ",4)'>Phóng viên</a>");
            if (!$("#btdoicauhoi").hasClass("Use")) {
                $("#btdoicauhoi").html("<a href='javascript:void(0)' style='display: block;' title='Đổi câu hỏi' onclick='doicauhoi(" + data.Level + ")'></a>");
            }
            if (!$("#bttrogiup50").hasClass("Use")) {
                $("#bttrogiup50").html("<a href='javascript:void(0)' style='display: block;' title='Trợ giúp 50/50' onclick='trogiup50(" + data.Level + ")'></a>");
            }
            if (!$("#btkhangia").hasClass("Use")) {
                $("#btkhangia").html("<a href='javascript:void(0)' style='display: block;' title='Hỏi ý kiến khán giả' onclick='khangia(" + data.Level + ")'></a>");
            }
            $("#blockQuestion").css("display", "block");
            $("#QuestionId").val(data.Id);
        }, 
        error: function () {
            alert("Some error occured.");
        }
    }); 
};
 
function hetgio() {
    playSound("out_of_time");
    PopupThongBao("THÔNG BÁO", "Hết giờ", " <div class='span1'><a href='javascript:void(0)' title='Đóng'  onclick='ketthuc()'>Đóng</a></div>");
     
}
function ketthuc() {
    popupNone();
    playcount = false; 
    mySwiper.swipeTo(5);
    playSound("lose");
}

function Answer(Id, Answer) {
    playcount = false;
    nhapnhay("Choice" + Answer + " a", 6, function () {

    }, "selected", "selected");
    
    playSound("ans_" + Answer+"1");
    setTimeout(function () {
        $.ajax({
            url: "/Home/AnswerJson/",
            type: "Post",
            data: { "Id": Id, "Answer": Answer },
            success: function (data) {
                timeplay = 2;
                if (data.IsTrue) {
                    playSound("true_" + data.Answer + "1");
                    nhapnhay("Choice" + Answer + " a", 6, function () {
                        playcount = false;
                        $("#Vnd").html(data.vnd);
                        $("#kgBlock").html("<div>CẢM ƠN BẠN ĐÃ THAM GIA CHƯƠNG TRÌNH</div><div>BẠN ĐÃ VƯỢT QUA CÂU HỎI SỐ <span>" + data.Level + "</span></div><div class='txt'>Thưởng câu hỏi</div><div class='thuong'>" + data.vnd + " VND</div>");

                        setTimeout(function () { 
                            if (data.Level == 5) {
                                playSound("chuc_mung_vuot_moc_01_0");
                                setTimeout(function () {
                                    question(data.Level + 1)
                                }, 7000);
                            } else if (data.Level == 10) {
                                playSound("chuc_mung_vuot_moc_02_0");
                                setTimeout(function () {
                                    question(data.Level + 1)
                                }, 5000);
                            } else {
                                question(data.Level + 1)
                            }
                        }, 2000);
                        
                    }, "true", "true");
                } else {
                    playSound("lose_" + data.Answer + "1");
                    nhapnhay("Choice" + Answer + " a", 3, function () {
                        nhapnhay("Choice" + data.Answer + " a", 6, function () {
                            setTimeout(function () {
                                ketthuc();
                            }, 5000); 
                        }, "true", "true");
                    }, "wrong", "wrong"); 
                } 
            },
            error: function () {
                alert("Some error occured.");
            }
        });
    }, 3000); 
}
 





















 
























function loadSound(name) {
    createjs.Sound.registerSound({ id: name, src: domain+"/Content/sounds/" + name + ".mp3" });
}



function playSound(name, loop) {
    createjs.Sound.stop();
    createjs.Sound.play(name); 
}

function touch_sound() {
    playSound("touch_sound");
} 
var countdown = function (htmlObjs, leftoverTime, callback) {
     playcount = true;
    var htmlObj = document.getElementById(htmlObjs);
    timeplay = 0;
    if (typeof (htmlObj) == 'object') {
        var thisObj = this;
        thisObj.timeHtmlObj = htmlObj;
        thisObj.time = leftoverTime;
        thisObj.updateCountdown = function () {
            if (playcount) {
                if (thisObj.time > 0) {
                    if (timeplay == 1) {// pause

                    }
                    if (timeplay == 2) {// resetTime
                        thisObj.time = 30;
                        timeplay = 0;
                    }
                    if (timeplay == 0) {//play
                        thisObj.timeHtmlObj.innerHTML = thisObj.time;
                        thisObj.time -= 1;
                    }
                    setTimeout(thisObj.updateCountdown, 1000);
                } else {
                    thisObj.timeHtmlObj.innerHTML = '0';
                    playcount = false;
                    return callback();
                }
            } else {
                thisObj.time =0;
                playcount = false;
            }
        }
        thisObj.updateCountdown();
    }
}
 
